import RSA from './rsa'
const crypto = require('crypto');
import {
	get,
	post,
	getUserProfile
} from './request.js'

export default {
	version:'2.0.0',
	user_token_key: '5KdQqxSw8NC5tC8na641yjDN8s6t2KLxtWJuoA5pijthS8jnmE3',
	navigationBarTitleText: '椰子问答',
	registerAD(cb){
		this.hanlderAD = cb
	},
	getTabBarHeight(){
		let gs = uni.getSystemInfoSync();
		let lift = gs.screenHeight - gs.safeArea.bottom
		let tabbarheight = Number(lift) + 50
		// #ifdef H5
		tabbarheight = 56
		// #endif
		return tabbarheight
	},
	getPageSize(){
		let gS = uni.getSystemInfoSync();
		return {
			width: gS.windowWidth,
			height: gS.windowHeight
		}
	},
	getADUnitId(cb){
		getUserProfile(() => {
			get('baseURL', '/setting/get',{
				key:'adUnitId_H5'
			}).then(res => {
				console.log(res,'getADUnitId')
				getApp().globalData.adUnitId = res.value.split(',')
				if(this.hanlderAD){
					this.hanlderAD(getApp().globalData.adUnitId)	
				}
				cb()
			})
		})
	},
	autoUpdate(){
		const updateManager = uni.getUpdateManager()
		updateManager.onCheckForUpdate(function (res) {
		  // 请求完新版本信息的回调
		  console.log(res.hasUpdate,'onCheckForUpdateonCheckForUpdate')
		})
		
		updateManager.onUpdateReady(function () {
		  uni.showModal({
		    title: '更新提示',
		    content: '新版本已经准备好，是否重启应用？',
		    success(res) {
		      if (res.confirm) {
		        // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
				console.log('新的版本已经下载好')
		        updateManager.applyUpdate()
		      }
		    }
		  })
		})
		
		updateManager.onUpdateFailed(function () {
		  // 新版本下载失败
		  console.log('新版本下载失败')
		})
	},
	onShareAppMessage(videoItem, source){
		if(videoItem.video_key){
			return {
			  title: this.navigationBarTitleText,
			  path: '/pages/videoInfo/index?video_key='+videoItem.video_key + '&isHome=true&source=' + source,
			}
		}else{
			return {
			  title: this.navigationBarTitleText,
			  path: '/pages/index/index',
			}
		}
	},
	login(cb){
		uni.login({
		  success:  (loginRes) => {
			  console.log(loginRes,'loginRes')
			if(loginRes.code){
				post('baseURL', '/wxUser/wx_login_get_openid',{
					code:loginRes.code
				}).then(result => {
					cb && cb(result)
				}).catch(err => {
					uni.showToast({
					    title: "微信登录授权失败" + err,
					    icon: "none"
					});
				})
			}
		  },
		  fail: (err) => {
			  uni.showToast({
			      title: "微信登录授权失败" + err,
			      icon: "none"
			  });
		  }
		});
	},
	isJSON(str){
		try{
			let data = JSON.parse(str)
			return true
		}catch(err){
			return false
		}
	},
	
	guid() {
	    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
	      var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
	      return v.toString(16);
	    });
	},
	parseTime(time, cFormat) {
	  if (arguments.length === 0) {
	    return null
	  }
	  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
	  let date
	  if (typeof time === 'object') {
	    date = time
	  } else {
	    if (('' + time).length === 10) time = parseInt(time) * 1000
	    date = new Date(time)
	  }
	  const formatObj = {
	    y: date.getFullYear(),
	    m: date.getMonth() + 1,
	    d: date.getDate(),
	    h: date.getHours(),
	    i: date.getMinutes(),
	    s: date.getSeconds(),
	    a: date.getDay()
	  }
	  const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
	    let value = formatObj[key]
	    if (key === 'a') return ['一', '二', '三', '四', '五', '六', '日'][value - 1]
	    if (result.length > 0 && value < 10) {
	      value = '0' + value
	    }
	    return value || 0
	  })
	  return time_str
	},
	getMaxZIndex(){
		let arr = [...document.all].map(e => +window.getComputedStyle(e).zIndex || 0);
		return arr.length ? Math.max(...arr) + 1 : 0
	}
}